<template>

  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
        ref="queryFormRef"
        :inline="true"
        :model="queryParams"
        class="-mb-15px"
        label-width="100px"
    >
      <el-form-item label="设备名称" prop="stuId">
        <el-input
            v-model.number="queryParams.stuId"
            class="!w-240px"
            clearable
            placeholder="请输入设备名称"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon class="mr-5px" icon="ep:search"/>
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon class="mr-5px" icon="ep:refresh"/>
          重置
        </el-button>
        <el-button plain type="primary" @click="openForm('create')">
          <Icon class="mr-5px" icon="ep:plus"/>
          新增
        </el-button>

<!--        <el-button :loading="exportLoading" plain type="success" @click="handleExport">-->
<!--          <Icon class="mr-5px" icon="ep:download"/>-->
<!--          导出-->
<!--        </el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column align="center" label="设备名称" prop="name"/>
      <el-table-column align="center" label="检修时间" prop="repTime"/>
      <el-table-column align="center" label="故障状态" prop="status">
        <template #default="scope">{{troubleStatusFilter(scope.row.status)}}</template>
      </el-table-column>
      <el-table-column align="center" label="检修人" prop="repPerson"/>
      <el-table-column align="center" label="检修费用" prop="repCost"/>
      <el-table-column align="center" label="备注" prop="remark"/>
      <el-table-column :width="300" align="center" label="操作">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update',scope.row.id)">修改</el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNo"
        :total="total"
        @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RoleForm ref="formRef" @success="getList"/>
  <!-- 表单弹窗：文件上传 -->
  <FileForm ref="formUpdateRef" @success="getList"/>
</template>
<script lang="ts" setup>
import download from '@/utils/download'
import * as RoleApi from '@/api/system/role'
import * as ProjectApi from '@/api/laboratory/report'
import RoleForm from './RoleForm.vue'
import FileForm from './UploadForm.vue'
import {getEquipmentNameList} from "@/api/laboratory/report";

defineOptions({name: 'SystemRole'})

const message = useMessage() // 消息弹窗
const {t} = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询维修设备列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ProjectApi.getEquipmentMaintainPage(queryParams)
    console.log(data)
    list.value = data.result
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}


/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProjectApi.deleteEquipmentMaintain({id: id})
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {
  }
}


// 获取设备维修状态
const getTroubleStatus = async () => {
  let aa = await ProjectApi.getEquipmentTypeList({pageNo: 1, pageSize: 100, dictType: 'troubleStatus'})
  let arr = []
  console.log('aa', aa)
  aa.list.forEach(item => {
    arr.push({
      label: item.label,
      value: item.value
    })
  })
  return arr;
}

const troubleStatusFilter = (value) => {
  let v = ""
  troubleStatusList.value.forEach(i => {
    if(i.value == value){
      v = i.label
    }
  })
  return v
}

// 设备故障状态列表
const troubleStatusList = ref([])
/** 初始化 **/
onMounted(async () => {
  getList()
  troubleStatusList.value = await getTroubleStatus()

})
</script>
